﻿package com.wandian.util
{
	import com.wandian.data.GameData;
	import com.wandian.util.loader.ImageLoader;
	import com.wandian.util.ImageLoaderSprite;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class CLZLoadIcon extends Object
	{
		private static const DICT:Dictionary=new Dictionary();

		public function CLZLoadIcon()
		{
			return;
		}

		public static function removeIcon(param1:String):void
		{
			if (DICT[param1])
			{
				delete DICT[param1];
			}
			return;
		}

		public static function addIcon2(parentMc:DisplayObjectContainer, url:String, iconW:int=0, iconH:int=0, iconX:Number=0, iconY:Number=0, initIcon:String="", isCenter:Boolean=false, isCache:Boolean=true):void
		{
			var mcContent:Sprite;
			var img:Bitmap;
			var bitmapData:BitmapData;
			var mc:MovieClip;
			mcContent=new Sprite();
			parentMc.addChild(mcContent);
			if (DICT[url] && isCache)
			{
				mc=DICT[url];
				parentMc.addChild(mc);
				mc.mask=null;

			}
			else
			{
				bitmapData=GameData.getBitMapDataByCache(url);
				if (bitmapData == null)
				{
					bitmapData=GameData.getBitMapDataByCache(initIcon);
				}
				img=new Bitmap(bitmapData);
				parentMc.addChild(img);
				if (url == "LoadingData")
				{
					mc.x=parentMc.width * 0.5;
					mc.y=parentMc.height * 0.5 - 20;
				}
				if (isCache)
				{
					DICT[url]=mc;
				}
			}

			if (iconW != 0)
			{
				if (iconH != 0)
				{
					CLZZoom.imageZoomWH(img, iconW, iconH);
				}
			}

			if (isCenter)
			{
				img.x=iconX + (parentMc.width - img.width) * 0.5;
				img.y=iconY + (parentMc.height - img.height) * 0.5;
			}
			else
			{
				img.x=iconX;
				img.y=iconY;
			}

			return;
		}

		public static function addIcon3(parentMc:DisplayObjectContainer, url:String, iconW:int=0, iconH:int=0, iconX:Number=0, iconY:Number=0, initIcon:String="", isCenter:Boolean=false, isCache:Boolean=true):void
		{
			var mcContent:Sprite;
			var img:Bitmap;
			var bitmapData:BitmapData;
			var mc:MovieClip;
			mcContent=new Sprite();
			parentMc.addChild(mcContent);
			if (DICT[url] && isCache)
			{
				mc=DICT[url];
				mcContent.addChild(mc);
				mc.mask=null;

			}
			else
			{
				bitmapData=GameData.getBitMapDataByCache(url);
				if (bitmapData == null)
				{
					mc=GameData.getMovieClip(initIcon);
					if (mc != null)
					{
						mcContent.addChild(mc);
					}
				}
				else
				{
					img=new Bitmap(bitmapData);
					mcContent.addChild(img);
					if (url == "LoadingData")
					{
						mc.x=parentMc.width * 0.5;
						mc.y=parentMc.height * 0.5 - 20;
					}
					if (isCache)
					{
						DICT[url]=mc;
					}
				}
			}

			if (iconW != 0)
			{
				if (iconH != 0)
				{
					CLZZoom.imageZoomWH(mcContent, iconW, iconH);
				}
			}

			if (isCenter)
			{
				mcContent.x=iconX + (parentMc.width - mcContent.width) * 0.5;
				mcContent.y=iconY + (parentMc.height - mcContent.height) * 0.5;
			}
			else
			{
				mcContent.x=iconX;
				mcContent.y=iconY;
			}

			return;
		}

		public static function addIcon(parentMc:DisplayObjectContainer, url:String, iconW:int=0, iconH:int=0, iconX:Number=0, iconY:Number=0, initIcon:String="", isCenter:Boolean=false, isCache:Boolean=true):Sprite
		{
			var mcContent:Sprite;
			var initMc:MovieClip;
			var image:ImageLoaderSprite;
			var progressFun:Function;
			var completeImageHandler:Function;
			var mc:DisplayObject;
			completeImageHandler=function(event:Event):void
			{
				if (isCache)
				{
					GameData.stopMcAllChild(image);
				}
				if (initMc != null)
				{
					if (parentMc.contains(initMc))
					{
						GameData.stopMcAllChild(initMc);
						mcContent.removeChild(initMc);
						initMc=null;
					}
				}

				if (iconW != 0)
				{
					if (iconH != 0)
					{
						CLZZoom.imageZoomWH(mcContent, iconW, iconH);
					}
				}

				if (isCenter)
				{
					mcContent.x=iconX + (parentMc.width - mcContent.width) * 0.5;
					mcContent.y=iconY + (parentMc.height - mcContent.height) * 0.5;
				}
				else
				{
					mcContent.x=iconX;
					mcContent.y=iconY;
				}
				return;
			} // end function
			;
			mcContent=new Sprite();
			parentMc.addChild(mcContent);
			if (DICT[url])
			{
				if (isCache)
				{
					mc=DICT[url];
					mcContent.addChild(mc);
					mc.mask=null;
					if (iconW != 0)
					{
						if (iconH != 0)
						{
							CLZZoom.imageZoomWH(mcContent, iconW, iconH);
						}
					}

					if (isCenter)
					{
						mcContent.x=iconX + (parentMc.width - mcContent.width) * 0.5;
						mcContent.y=iconY + (parentMc.height - mcContent.height) * 0.5;
					}
					else
					{
						mcContent.x=iconX;
						mcContent.y=iconY;
					}
					return mcContent;
				}
			}

			if (initIcon != "")
			{
				initMc=GameData.getMovieClip(initIcon);
				if (initMc != null)
				{
					mcContent.addChild(initMc);
					mcContent.x=iconX;
					mcContent.y=iconY;
					if (initIcon == "LoadingData")
					{
						initMc.x=parentMc.width * 0.5;
						initMc.y=parentMc.height * 0.5 - 20;
					}
				}
			}
			image=new ImageLoaderSprite();
			image.addEventListener(Event.COMPLETE, completeImageHandler);
			image.load(url, iconW, iconH);
			if (isCache)
			{
				DICT[url]=image;
			}
			mcContent.addChild(image);
			return mcContent;
		}

		public static function addBytesIcon(parentMc:DisplayObjectContainer, bytes:ByteArray, iconW:int=0, iconH:int=0, iconX:Number=0, iconY:Number=0, initIcon:String="", isCenter:Boolean=false, isCache:Boolean=true):Sprite
		{
			var mcContent:Sprite;
			var initMc:MovieClip;
			var image:ImageLoader;
			var progressFun:Function;
			var completeImageHandler:Function;
			var mc:DisplayObject;
			completeImageHandler=function(event:Event):void
			{
				if (initMc != null)
				{
					if (parentMc.contains(initMc))
					{
						GameData.stopMcAllChild(initMc);
						mcContent.removeChild(initMc);
						initMc=null;
					}
				}
				var image:ImageLoader=event.target as ImageLoader;
				mcContent.addChild(new Bitmap(image.bitmapdata));
				image=null;
				if (iconW != 0 && iconH != 0)
				{
					CLZZoom.imageZoomWH(mcContent, iconW, iconH);
				}

				if (isCenter)
				{
					mcContent.x=iconX + (parentMc.width - mcContent.width) * 0.5;
					mcContent.y=iconY + (parentMc.height - mcContent.height) * 0.5;
				}
				else
				{
					mcContent.x=iconX;
					mcContent.y=iconY;
				}
				return;
			};
			mcContent=new Sprite();
			parentMc.addChild(mcContent);
//			if (initIcon != "")
//			{
//				initMc=GameData.getMovieClip(initIcon);
//				if (initMc != null)
//				{
//					mcContent.addChild(initMc);
//					mcContent.x=iconX;
//					mcContent.y=iconY;
//					if (initIcon == "LoadingData")
//					{
//						initMc.x=parentMc.width * 0.5;
//						initMc.y=parentMc.height * 0.5 - 20;
//					}
//				}
//			}
			image=new ImageLoader();
			image.addEventListener(Event.COMPLETE, completeImageHandler);
			image.loadBytes(bytes);
			return mcContent;
		}

	}
}
